package com.syscom.server.qd.ui;

import java.io.IOException;
import java.sql.SQLException;

import com.syscom.safe.wac.buf.SacInputBuffer;
import com.syscom.safe.wac.buf.SacOutputBuffer;
import com.syscom.safe.wac.exception.WacException;
import com.syscom.safe.wac.txbean.TxContext;
import com.syscom.sar.server.constant.QDCActFuncConstant;
import com.syscom.server.qd.cacheManager.RuleQtaCacheManager;
import com.syscom.server.qd.vo.RuleQtaStatusVo;
import com.syscom.shared.server.action.Action;
import com.syscom.shared.vo.ResponseStatus;

public class QuotaAction extends Action {

	public void executeAsyncRequest(TxContext ctx, SacInputBuffer req) throws WacException, SQLException, IOException {
	}

	public byte[] executeSyncRequest(TxContext ctx, SacInputBuffer req) throws WacException, SQLException, IOException {
		String sFuncCd = req.readShortString();
		
		if(sFuncCd.equals(QDCActFuncConstant.QTA_FUNC_GET_INIT_QUOTA)) {
			return getInitQuota(ctx, req);
		}
		else if(sFuncCd.equals(QDCActFuncConstant.QTA_FUNC_GET_QUOTA)) {
			return getQuota(ctx, req);
		}
		else if(sFuncCd.equals(QDCActFuncConstant.QTA_FUNC_GET_QUOTA_DRAW_BACK)) {
			return getQuotaAfterDrawBack(ctx, req);
		}
		else if(sFuncCd.equals(QDCActFuncConstant.QTA_FUNC_ROLL_BACK)) {
			return rollBack(ctx, req);
		}
		
		return null;
	}

	private byte[] getInitQuota(TxContext ctx, SacInputBuffer req) throws WacException, IOException {
		SacOutputBuffer outBuffer = new SacOutputBuffer();
		
		String sServerId = req.readAutoString();
		
		RuleQtaStatusVo[] vos = RuleQtaCacheManager.getInstance().getInitQuotaByServerId(sServerId);
		
		outBuffer.writeByte(ResponseStatus.OK);
		
		int iLength = vos.length;
		outBuffer.writeInt(iLength);
		for(int i = 0 ; i < iLength ; i++) {
			vos[i].fillOutBuffer(outBuffer);
		}
		
		return outBuffer.getByteArray();
	}
	
	private byte[] getQuota(TxContext ctx, SacInputBuffer req) throws WacException, IOException {
		SacOutputBuffer outBuffer = new SacOutputBuffer();
		
		// TODO
		String sServerId = req.readAutoString();
		int iRuleId = req.readInt();
		String sProdId = req.readAutoString();
		double dNeedQuota = req.readDouble();
		boolean bIsLockProd = req.readBoolean();
		
		RuleQtaStatusVo ruleQtaStatusVo = RuleQtaCacheManager.getInstance().getQuota(sServerId, iRuleId, sProdId, dNeedQuota, bIsLockProd);
		
		outBuffer.writeByte(ResponseStatus.OK);
		
		ruleQtaStatusVo.fillOutBuffer(outBuffer);
			
		return outBuffer.getByteArray();
	}

	private byte[] getQuotaAfterDrawBack(TxContext ctx, SacInputBuffer req) throws WacException, IOException {
		SacOutputBuffer outBuffer = new SacOutputBuffer();
		
		int iRuleId = req.readInt();
		String sProdId = req.readShortString();
		double dNeedQuota = req.readDouble();
		
		RuleQtaStatusVo ruleQtaStatusVo = RuleQtaCacheManager.getInstance().getQuotaAfterDrawBack(iRuleId, sProdId, dNeedQuota);
		
		outBuffer.writeByte(ResponseStatus.OK);
		
		ruleQtaStatusVo.fillOutBuffer(outBuffer);
		
		return outBuffer.getByteArray();
	}
	
	private byte[] rollBack(TxContext ctx, SacInputBuffer req) throws WacException, IOException {
		SacOutputBuffer outBuffer = new SacOutputBuffer();
		
		int iRuleId = req.readInt();
		String sProdId = req.readShortString();
		double dRollBackValue = req.readDouble();
		
		RuleQtaCacheManager.getInstance().rollBackToQD(iRuleId, sProdId, dRollBackValue);
		
		outBuffer.writeByte(ResponseStatus.OK);
		
		return outBuffer.getByteArray();
	}
}
